import 'package:flutter/material.dart';
import 'package:love_app/enum/bind_lover.dart';
import 'package:love_app/enum/login_status.dart';
import 'package:love_app/enum/sex.dart';
import 'package:love_app/model/user_info_model.dart' as USModel;
import 'package:love_app/service/form_token_api.dart';


// 自定义一个全局共享的模型
class ShareProvider with ChangeNotifier {
  List<Color> _themeColor; // 主题颜色
  Color _textColor; // 字体颜色
  String _token; // token值
  Sex _sex; // 性别
  LoginStatus _loginStatus; // 登录状态
  int _uid; // 用户在数据库对应的uid
  String _formToken; // 防止表单重复提交的token
  USModel.Lover_info _loverInfo; // 另一半的信息
  BindLover _bindLover; // 有没有绑定
  String _nickname; // 自己的昵称
  String _phone; // 手机号码
  String _headerImage; // 头像地址
  bool _copyData = false; // 剪切板的值是否已经使用，如果使用的话，则不然再弹出


  List<Color> get themeColor => this._themeColor;
  Color get textColor => this._textColor;
  String get token => this._token;
  Sex get sex => this._sex;
  LoginStatus get loginStatus => this._loginStatus;
  int get uid => this._uid;
  String get formToken => this._formToken;
  USModel.Lover_info get loverInfo => this._loverInfo;
  BindLover get bindLover => this._bindLover;
  String get nickname => this._nickname;
  String get phone => this._phone;
  String get headerImage => this._headerImage;
  bool get copyData => this._copyData;

  set copyData(bool copyData){
    this._copyData = copyData;
  }

  set themeColor(List<Color> themeColor) {
    this._themeColor = themeColor;
  }

  set textColor(Color textColor) {
    this._textColor = textColor;
  }

  set token(String token) {
    this._token = token;
  }

  set sex(Sex sex) {
    this._sex = sex;
  }

  set loginStatus(LoginStatus loginStatus) {
    this._loginStatus = loginStatus;
  }

  set uid(int uid) {
    this._uid = uid;
  }

  set formToken(String formToken) {
    this._formToken = formToken;
  }

  set loverInfo(USModel.Lover_info loverInfo) {
    this._loverInfo = loverInfo;
  }

  set bindLover(BindLover bindLover) {
    this._bindLover = bindLover;
  }

  set nickname(String nickname) {
    this._nickname = nickname;
  }

  set phone(String phone) {
    this._phone = phone;
  }

  set headerImage(String headerImage) {
    this._headerImage = headerImage;
  }

  // 初始化
  ShareProvider.init()
      : this._themeColor = [
          Color.fromRGBO(239, 144, 194, 1),
          Color.fromRGBO(238, 131, 165, 1)
        ],
        this._textColor = Color.fromRGBO(255, 175, 196, 1),
        this._token = '',
        this._sex = Sex.girl,
        this._loginStatus = LoginStatus.noLogin,
        this._bindLover = BindLover.noBing,
        this._loverInfo = null,
        this._nickname = null,
        this._phone = null,
        this._headerImage = null;

  // 赋值
  ShareProvider.addData(
    String token,
    Sex sex,
    LoginStatus status,
    int uid,
    String formToken,
    BindLover bindLover,
    USModel.Lover_info loverInfo,
    String nickname,
    String phone,
    String headerImage,
  ) {
    this._token = token;
    this._sex = sex;
    this._loginStatus = status;
    this._uid = uid;
    this._formToken = formToken;
    this._themeColor = sex == Sex.girl
        ? [Color.fromRGBO(239, 144, 194, 1), Color.fromRGBO(238, 131, 165, 1)]
        : [Color.fromRGBO(134, 178, 238, 1), Color.fromRGBO(130, 190, 255, 1)];
    this._textColor = sex == Sex.girl
        ? Color.fromRGBO(255, 175, 196, 1)
        : Color.fromRGBO(125, 207, 249, 1);
    this._bindLover = bindLover;
    this._loverInfo = loverInfo;
    this._nickname = nickname;
    this._phone = phone;
    this._headerImage = headerImage;
  }

  /// 初始化模型
  void loginInit(
    String token,
    Sex sex,
    LoginStatus status,
    int uid,
    String formToken,
    BindLover bindLover,
    USModel.Lover_info loverInfo,
    String nickname,
    String phone,
    String headerImage,
  ) {
    this._token = token;
    this._sex = sex;
    this._loginStatus = status;
    this._themeColor = sex == Sex.girl
        ? [Color.fromRGBO(239, 144, 194, 1), Color.fromRGBO(238, 131, 165, 1)]
        : [Color.fromRGBO(134, 178, 238, 1), Color.fromRGBO(130, 190, 255, 1)];
    this._textColor = sex == Sex.girl
        ? Color.fromRGBO(255, 175, 196, 1)
        : Color.fromRGBO(125, 207, 249, 1);
    this._uid = uid;
    this._formToken = formToken;
    this._bindLover = bindLover;
    this._loverInfo = loverInfo;
    this._nickname = nickname;
    this._phone = phone;
    this._headerImage = headerImage;
  }

  // 更新formToken的值
  updateFormToken() async {
    String newFormToken = await FormTokenApi().getFormToken(this.token);
    this._formToken = newFormToken;
  }

  // 清除model
  void cleanModel() {
    this._token = null;
    this._sex = null;
    this._loginStatus = null;
    this._themeColor = null;
    this._themeColor = null;
    this._uid = null;
    this._formToken = null;
    this._bindLover = null;
    this._loverInfo = null;
    this._nickname = null;
    this._phone = null;
    this._headerImage = null;
  }
}
